import { newCertApi } from "../api/apply";
export function phoneCheck(val: any) {
  let reg = /(^1[3|4|5|7|8|9]\d{9}$)|(^09\d{8}$)/;
  if (!val) {
    return false;
  }
  if (!reg.test(val)) {
    return false;
  } else {
    return true;
  }
}

export function checkName(val) {
  if (!val) {
    return "请输入姓名";
  }
  if (val) {
    var reg = /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/;
    if (!reg.test(val)) {
      return "请输入正确的姓名";
    } else {
      return "ok";
    }
  }
}

//6到18位密码
export function passwordCheck(val: any) {
  let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/;
  if (!val) {
    return false;
  }
  if (!reg.test(val)) {
    return false;
  } else {
    return true;
  }
}

export function idCardCheck(idcode) {
  // 加权因子
  var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  // 校验码
  var check_code = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];

  var code = idcode + "";
  var last = idcode[17]; //最后一位
  var seventeen = code.substring(0, 17);
  // ISO 7064:1983.MOD 11-2
  // 判断最后一位校验码是否正确
  var arr = seventeen.split("");
  var len = arr.length;
  var num = 0;
  for (var i = 0; i < len; i++) {
    num = num + arr[i] * weight_factor[i];
  }
  // 获取余数
  var resisue = num % 11;
  var last_no = check_code[resisue];

  var idcard_patter =
    /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;

  // 判断格式是否正确
  var format = idcard_patter.test(idcode);

  // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码

  return last === last_no && format ? true : false;
}

export function applyInfoCheck(row) {
  var types = [];
  var message = {
    isError: false,
    types: types,
    errorMsg: "",
  };
  //姓名
  if (!row.newCert.name) {
    types.push("name");
    message = {
      isError: true,
      types,
    };
  } else {
    let result = checkName(row.newCert.name);
    if (result != "ok") {
      types.push("nameFormat");
      message = {
        isError: true,
        types,
      };
    } else {
    }
  }
  //民族
  if (!row.newCert.nation) {
    types.push("nation");
    message = {
      isError: true,
      types,
    };
  } else {
    let result = checkName(row.newCert.name);
    if (result != "ok") {
      types.push("nameFormat");
      message = {
        isError: true,
        types,
      };
    }
  }
  //手机号
  if (!row.newCert.mobilePhone) {
    types.push("mobilePhone");
    message = {
      isError: true,
      types,
    };
  } else {
    let result = phoneCheck(row.newCert.mobilePhone);
    if (!result) {
      types.push("mobilePhoneFormat");
      message = {
        isError: true,
        types,
      };
    }
  }

  //户口性质
  if (!row.newCert.householdType) {
    types.push("householdType");
    message = {
      isError: true,
      types,
    };
  }
  // //文化程度
  if (!row.newCert.education) {
    types.push("education");
    message = {
      isError: true,
      types,
    };
  }
  // //婚姻情况
  if (!row.newCert.marriage) {
    types.push("marriage");
    message = {
      isError: true,
      types,
    };
  }
  // //户籍地所属区划
  if (!row.newCert.areaCode) {
    types.push("areaCode");
    message = {
      isError: true,
      types,
    };
  } else {
    if (row.newCert.areaCode.length < 12) {
      message = {
        isError: true,
        types,
        errorMsg: "户籍地所属区划必须到村（社区）级",
      };
    }
  }

  // //户籍地地址
  if (!row.newCert.permanentAddress) {
    types.push("permanentAddress");
    message = {
      isError: true,
      types,
    };
  }
  // //居住地所属区划码
  if (!row.newCert.residenceCode) {
    types.push("residenceCode");
    message = {
      isError: true,
      types,
    };
  } else {
    if (row.newCert.residenceCode.length < 12) {
      types.push("residenceCodeFormat");
      message = {
        isError: true,
        types,
      };
    }
  }
  // //居住地地址
  if (!row.newCert.residenceAddress) {
    types.push("residenceAddress");
    message = {
      isError: true,
      types,
    };
  }
  // 申请残疾类别
  if (!row.newCert.deformityType || !row.newCert.deformityType.length) {
    types.push("deformityType");
    message = {
      isError: true,
      types,
    };
  }
  if (row.matterInfo.model == 2) {
    if (!row.guardian.name) {
      types.push("guardianName");
      message = {
        isError: true,
        types,
      };
    }
    if (!row.guardian.idcard) {
      types.push("guardianIdcard");
      message = {
        isError: true,
        types,
      };
    } else if (!idCardCheck(row.guardian.idcard)) {
      types.push("guardianIdcard");
      message = {
        isError: true,
        types,
      };
    }
    if (row.newCert.idcard == row.guardian.idcard) {
      types.push("guardianIdcardValidateSame");
      message = {
        isError: true,
        types,
      };
    }
    if (!row.guardian.relation) {
      types.push("guardianRelation");
      message = {
        isError: true,
        types,
      };
    }

    if (!row.guardian.mobilePhone) {
      types.push("guardianMobilePhone");
      message = {
        isError: true,
        types,
      };
    } else {
      let result = phoneCheck(row.guardian.mobilePhone);
      if (!result) {
        types.push("guardianMobilePhoneFormat");
        message = {
          isError: true,
          types,
        };
      }
    }
  }

  if (row.matterInfo.model == 3) {
    if (!row.agent.name) {
      types.push("agentName");
      message = {
        isError: true,
        types,
      };
    }
    if (!row.agent.idcard) {
      types.push("agentIdcard");
      message = {
        isError: true,
        types,
      };
    } else if (!idCardCheck(row.agent.idcard)) {
      types.push("agentIdcard");
      message = {
        isError: true,
        types,
      };
    }
    if (row.newCert.idcard == row.agent.idcard) {
      types.push("agentIdcardValidateSame");
      message = {
        isError: true,
        types,
      };
    }
    if (!row.agent.relation) {
      types.push("agentRelation");
      message = {
        isError: true,
        types,
      };
    }
    if (!row.agent.mobilePhone) {
      types.push("agentMobilePhone");
      message = {
        isError: true,
        types,
      };
    } else {
      let result = phoneCheck(row.agent.mobilePhone);
      if (!result) {
        types.push("agentMobilePhoneFormat");
        message = {
          isError: true,
          types,
        };
      }
    }
  }

  if (row.matterType == "destroy") {
    if (!row.newCert.reason) {
      types.push("reason");
      message = {
        isError: true,
        types,
      };
    }
  }

  return message;
}

export function applyInfoCheckItem(row, name) {
  var message = {
    isError: false,
    errorMsg: "",
  };
  switch (name) {
    //姓名
    case "name":
      if (!row.newCert.name) {
        message = {
          isError: true,
        };
      } else {
        let result = checkName(row.newCert.name);
        if (result != "ok") {
          message = {
            isError: true,
            errorType: "nameFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "nameFormat",
          };
        }
      }
      return message;
    //民族
    case "nation":
      if (!row.newCert.nation) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    //手机号
    case "mobilePhone":
      if (!row.newCert.mobilePhone) {
        message = {
          isError: true,
        };
      } else {
        let result = phoneCheck(row.newCert.mobilePhone);
        if (!result) {
          message = {
            isError: true,
            errorType: "mobilePhoneFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "mobilePhoneFormat",
          };
        }
      }
      return message;
    //户口性质
    case "householdType":
      if (!row.newCert.householdType) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    //文化程度
    case "education":
      if (!row.newCert.education) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    //婚姻情况
    case "marriage":
      if (!row.newCert.marriage) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    // 户籍地所属区划
    case "areaCode":
      if (!row.newCert.areaCode) {
        message = {
          isError: true,
        };
      } else {
        if (row.newCert.areaCode.length < 12) {
          message = {
            isError: true,
            errorType: "areaCodeFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "areaCodeFormat",
          };
        }
      }
      return message;

    // //户籍地地址
    case "permanentAddress":
      if (!row.newCert.permanentAddress) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    // 居住地所属区划
    case "residenceCode":
      if (!row.newCert.residenceCode) {
        message = {
          isError: true,
        };
      } else {
        if (row.newCert.residenceCode.length < 12) {
          message = {
            isError: true,
            errorType: "residenceCodeFormat",
          };
        } else {
          message = {
            isError: false,
            errorType3: "residenceCodeFormat",
          };
        }
      }
      return message;

    //居住地地址
    case "residenceAddress":
      if (!row.newCert.residenceAddress) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    // 申请残疾类别
    case "deformityType":
      if (!row.newCert.deformityType || !row.newCert.deformityType.length) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    // 申请残疾类别
    case "deformityType":
      if (!row.newCert.deformityType || !row.newCert.deformityType.length) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    //监护人或联系人姓名
    case "guardianName":
      if (!row.guardian.name) {
        message = {
          isError: true,
        };
      } else {
        let result = checkName(row.guardian.name);
        if (result != "ok") {
          message = {
            isError: true,
            errorType: "guardianNameFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "guardianNameFormat",
          };
        }
      }
      return message;

    //监护人或联系人身份证
    case "guardianIdcard":
      if (!row.guardian.idcard) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
        let result = idCardCheck(row.guardian.idcard);
        if (!result) {
          message = {
            isError: true,
            errorType2: "guardianIdcardValidateSame",
          };
          return message;
        } else {
          message = {
            isError: false,
            errorType3: "guardianIdcardValidateSame",
          };

          if (row.newCert.idcard == row.guardian.idcard) {
            message = {
              isError: true,
              errorType: "guardianIdcardValidateSame",
            };
          } else {
            message = {
              isError: false,
              errorType: "guardianIdcardValidateSame",
            };
          }
        }
      }
      return message;

    // 监护人或联系人与申请人关系
    case "guardianRelation":
      if (!row.guardian.relation) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    // 监护人或联系人联系方式
    case "guardianMobilePhone":
      if (!row.guardian.mobilePhone) {
        message = {
          isError: true,
        };
      } else {
        let result = phoneCheck(row.guardian.mobilePhone);
        if (!result) {
          message = {
            isError: true,
            errorType: "guardianMobilePhoneFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "guardianMobilePhoneFormat",
          };
        }
      }
      return message;

    //帮办人姓名
    case "agentName":
      if (!row.agent.name) {
        message = {
          isError: true,
        };
      } else {
        let result = checkName(row.agent.name);
        if (result != "ok") {
          message = {
            isError: true,
            errorType: "agentNameFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "agentNameFormat",
          };
        }
      }
      return message;

    //帮办人身份证
    case "agentIdcard":
      if (!row.agent.idcard) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
        let result = idCardCheck(row.agent.idcard);
        if (!result) {
          message = {
            isError: true,
            errorType2: "agentIdcardValidateSame",
          };
          return message;
        } else {
          message = {
            isError: false,
            errorType3: "agentIdcardValidateSame",
          };

          if (row.newCert.idcard == row.agent.idcard) {
            message = {
              isError: true,
              errorType: "agentIdcardValidateSame",
            };
          } else {
            message = {
              isError: false,
              errorType: "agentIdcardValidateSame",
            };
          }
        }
      }
      return message;

    // 帮办人与申请人关系
    case "agentRelation":
      if (!row.agent.relation) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;

    // 帮办人联系方式
    case "agentMobilePhone":
      if (!row.agent.mobilePhone) {
        message = {
          isError: true,
        };
      } else {
        let result = phoneCheck(row.agent.mobilePhone);
        if (!result) {
          message = {
            isError: true,
            errorType: "agentMobilePhoneFormat",
          };
        } else {
          message = {
            isError: false,
            errorType: "agentMobilePhoneFormat",
          };
        }
      }
      return message;

    //原因
    case "reason":
      if (!row.newCert.reason) {
        message = {
          isError: true,
        };
      } else {
        message = {
          isError: false,
        };
      }
      return message;
  }
}

function getUpload10ShowStatus(row) {
  if (row.newCert.areaCode && row.newCert.residenceCode) {
    if (row.newCert.areaCode.toString().substring(0, 2) == 33) {
      return false;
    } else {
      if (
        row.newCert.areaCode.toString().length >= 6 &&
        row.newCert.residenceCode.toString().length >= 6
      ) {
        if (
          row.newCert.areaCode.toString().substring(0, 6) !=
          row.newCert.residenceCode.toString().substring(0, 6)
        ) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }
  } else {
    return false;
  }
}

export function applyImageCheck(row, store) {
  let upload10Show = getUpload10ShowStatus(row);
  const isFuYang = uni
    .getStorageSync("newcert-mobile-areaCode")
    .includes("330183")
    ? true
    : false;


  const isCreatChange =
    row.matterInfo.matterCode == "10ALLEqnBPzLBPiPUBnBWsUCOd0c38zj_ck" || row.matterInfo.matterCode == "RS6vj7wb5ucI4iEpJZS63eZPwLdi64Dh_ck";
  if (row.matterInfo.model == 1) {
    if (
      !row.uploadData.uploadData1.srcList
        .map((val) => {
          return val.url;
        })
        .toString() &&
      !row.uploadData.uploadData2.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传申请人身份证或户口簿（二选一）";
    }
    if (
      !row.uploadData.uploadData5.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传申请人照片";
    } else {
      if (
        row.uploadData.uploadData5.srcList.map((val) => {
          return val.url;
        }).length > 1
      ) {
        return "申请人照片只能上传一张";
      }
    }
    if (upload10Show) {
      if (
        !row.uploadData.uploadData10.srcList
          .map((val) => {
            return val.url;
          })
          .toString() &&
        !row.uploadData.uploadData10.srcList
          .map((val) => {
            return val.url;
          })
          .toString()
      ) {
        return "请先上传居住证";
      }
    }


    if (isFuYang && isCreatChange) {
      if (
        !row.uploadData.uploadData11.srcList
          .map((val) => {
            return val.url;
          })
          .toString() &&
        !row.uploadData.uploadData11.srcList
          .map((val) => {
            return val.url;
          })
          .toString()
      ) {
        return "请先上传病例资料";
      }
    }
  }

  if (row.matterInfo.model == 2) {
    if (
      !row.uploadData.uploadData1.srcList
        .map((val) => {
          return val.url;
        })
        .toString() &&
      !row.uploadData.uploadData2.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传申请人身份证或户口簿（二选一）";
    }
    if (
      !row.uploadData.uploadData5.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传申请人照片";
    } else {
      if (
        row.uploadData.uploadData5.srcList.map((val) => {
          return val.url;
        }).length > 1
      ) {
        return "申请人照片只能上传一张";
      }
    }
    if (upload10Show) {
      if (
        !row.uploadData.uploadData10.srcList
          .map((val) => {
            return val.url;
          })
          .toString() &&
        !row.uploadData.uploadData10.srcList
          .map((val) => {
            return val.url;
          })
          .toString()
      ) {
        return "请先上传居住证";
      }
    }
    if (isFuYang && isCreatChange) {
      if (
        !row.uploadData.uploadData11.srcList
          .map((val) => {
            return val.url;
          })
          .toString() &&
        !row.uploadData.uploadData11.srcList
          .map((val) => {
            return val.url;
          })
          .toString()
      ) {
        return "请先上传病例资料";
      }
    }
    if (
      !row.uploadData.uploadData3.srcList
        .map((val) => {
          return val.url;
        })
        .toString() &&
      !row.uploadData.uploadData4.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传监护人或联系人 身份证或户口簿（二选一）";
    }
  }

  if (row.matterInfo.model == 3) {
    if (
      !row.uploadData.uploadData1.srcList
        .map((val) => {
          return val.url;
        })
        .toString() &&
      !row.uploadData.uploadData2.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传申请人身份证或户口簿（二选一）";
    }
    if (
      !row.uploadData.uploadData5.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传申请人照片";
    } else {
      if (
        row.uploadData.uploadData5.srcList.map((val) => {
          return val.url;
        }).length > 1
      ) {
        return "申请人照片只能上传一张";
      }
    }
    if (upload10Show) {
      if (
        !row.uploadData.uploadData10.srcList
          .map((val) => {
            return val.url;
          })
          .toString() &&
        !row.uploadData.uploadData10.srcList
          .map((val) => {
            return val.url;
          })
          .toString()
      ) {
        return "请先上传居住证";
      }
    }
    if (isFuYang && isCreatChange) {
      if (
        !row.uploadData.uploadData11.srcList
          .map((val) => {
            return val.url;
          })
          .toString() &&
        !row.uploadData.uploadData11.srcList
          .map((val) => {
            return val.url;
          })
          .toString()
      ) {
        return "请先上传病例资料";
      }
    }
    if (
      !row.uploadData.uploadData6.srcList
        .map((val) => {
          return val.url;
        })
        .toString() &&
      !row.uploadData.uploadData7.srcList
        .map((val) => {
          return val.url;
        })
        .toString()
    ) {
      return "请先上传帮办人 身份证或户口簿（二选一）";
    }
  }
}

export function applyWayCheck(row) {
  if (!row.matterInfo.way) {
    return "请先选择取件方式";
  }
  if (row.matterInfo.way == 3) {
    if (!row.addressPost.address) {
      return "请先选择邮寄地址";
    }
  }
}
